package com.ruoyi.web.controller.hsz;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.HszLetter;
import com.ruoyi.system.service.IHszLetterService;
import com.ruoyi.web.controller.hsz.param.HszLetterPageParam;
import com.ruoyi.web.controller.hsz.param.HszLetterSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

import static com.ruoyi.system.domain.table.HszLetterTableDef.HSZ_LETTER;

/**
 * 会长信箱
 *
 * @author zhangqingshan
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/hsz/letter")
@Slf4j
public class HszLetterController {

    @Autowired
    private IHszLetterService hszLetterService;

    /**
     * 提交
     *
     * @param param 参数
     * @return {@link R }
     */
    @Anonymous
    @PostMapping("/submit")
    @Transactional(rollbackFor = Exception.class)
    public R submit(@RequestBody @Validated HszLetterSubmitParam param) {
        hszLetterService.validateCaptcha(param.getUuid(), param.getCode()); // 校验验证码
        HszLetter letter = param.toDO();
        return hszLetterService.save(letter) ? R.ok() : R.fail();
    }

    /**
     * 删除
     * 权限：hsz:letter:del
     *
     * @param letterId 信箱id
     * @return {@link R }
     */
    @PostMapping("/delete/{letterId}")
    @PreAuthorize("@ss.hasPermi('hsz:letter:del')")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@PathVariable("letterId") Long letterId) {
        return hszLetterService.removeById(letterId) ? R.ok() : R.fail();
    }

    /**
     * 详情
     * 权限：hsz:letter:list
     *
     * @param letterId 信箱id
     * @return {@link R }
     */
    @GetMapping("/detail/{letterId}")
    @PreAuthorize("@ss.hasPermi('hsz:letter:list')")
    public R<HszLetter> detail(@PathVariable("letterId") Long letterId) {
        HszLetter letter = hszLetterService.getById(letterId);
        return R.ok(letter);
    }

    /**
     * 分页
     * 权限：hsz:letter:list
     *
     * @param param 参数
     * @return {@link R }
     */
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermi('hsz:letter:list')")
    public R<Page<HszLetter>> page(@RequestBody @Validated HszLetterPageParam param) {
        return R.ok(hszLetterService.page(param.toPage(), QueryWrapper.create().from(HszLetter.class)
                .where(HSZ_LETTER.NAME.like(param.getName()).when(StringUtils.isNotBlank(param.getName())))
                .and(HSZ_LETTER.TITLE.like(param.getTitle()).when(StringUtils.isNotBlank(param.getTitle())))
                .and(HSZ_LETTER.MOBILE.eq(param.getMobile()).when(StringUtils.isNotBlank(param.getMobile())))
                .and(HSZ_LETTER.TEL.eq(param.getTel()).when(StringUtils.isNotBlank(param.getTel())))
                .and(HSZ_LETTER.EMAIL.eq(param.getEmail()).when(StringUtils.isNotBlank(param.getEmail())))
                .and(HSZ_LETTER.CREATE_TIME.ge(param.getCreateTimeStart()).when(Objects.nonNull(param.getCreateTimeStart())))
                .and(HSZ_LETTER.CREATE_TIME.le(param.getCreateTimeEnd()).when(Objects.nonNull(param.getCreateTimeEnd())))
                .orderBy(HSZ_LETTER.CREATE_TIME.desc())));
    }


}
